#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "cgi_interface.h"
#include "global_val.h"
#include "alarm_task.h"

/*********************************************************************
 * @fn      alarm_task_creat_head
 *
 * @brief   初始化一个alarm_task头结点
 *
 * @param   none
 *
 * @return  返回初始化的alarm_task头结点
 */
alarm_task_head_t *alarm_task_creat_head(void)
{
    alarm_task_head_t *head;

    head       = (alarm_task_head_t *)malloc(sizeof(alarm_task_head_t));
    head->head = NULL;
    head->tail = NULL;

    return head;
}

/*********************************************************************
 * @fn      insert_alarm_task
 *
 * @brief   向alarm_head插入一个节点
 *
 * @param   alarm_head:头结点
 * @param   info:存放定时数据
 *
 * @return  none
 */
void insert_alarm_task(alarm_task_head_t *alarm_head, alarm_info_t *info)
{
    alarm_task_node_t *node;

    printf("mac:%s alarm_time:%s id:%d\n", info->mac, info->alarm_time, info->alarm_id);

    if(alarm_head==NULL)
    {
        return ;
    }

    node       = (alarm_task_node_t *)malloc(sizeof(alarm_task_node_t));
    node->next = NULL;
    memcpy(&node->alarm_info, info, sizeof(alarm_info_t));

    if(alarm_head->head==NULL)
    {
        //first insert
        alarm_head->head = node;
        alarm_head->tail = node;
    }
    else
    {
        //not first insert
        alarm_head->tail->next = node;
        alarm_head->tail       = node;

    }
}

/*********************************************************************
 * @fn      loop_alarm_task
 *
 * @brief   遍历alarm_head,找出定时时间到达的节点,执行回调函数并删除
 *          节点
 *
 * @param   alarm_head:头结点
 *
 * @return  none
 */
void loop_alarm_task(alarm_task_head_t *alarm_head)
{
    time_t tNow;
    struct tm *ptm;
    char ntime[32];
    alarm_task_node_t *node;

    if(alarm_head==NULL || alarm_head->head==NULL)
    {
        return ;
    }

    node      = alarm_head->head;
    while(node!=NULL)
    {
        if(node->alarm_info.alarm_switch==ALARM_ON)
        {
            tNow = time(NULL);
            ptm  = localtime(&tNow);
            strftime(ntime, 32, "%H:%M", ptm);

            printf("ntime:%s\n", ntime);

            if(strcmp(ntime, node->alarm_info.alarm_time)==0)
            {
                //调用回调函数
                node->alarm_info.alarm_cb(node->alarm_info.mac, node->alarm_info.status);

                if(node->alarm_info.alarm_repeat==ALARM_NONREPEAT)
                {
                    node->alarm_info.alarm_switch = ALARM_OFF;
                    //数据库 更新闹钟状态
                    update_intfield_value_by_intfield("alarm_task", "ID", node->alarm_info.alarm_id, "alarm_switch", ALARM_OFF);
                }
            }
        }
        //指向下一个节点
        node = node->next;
    }
}

/*********************************************************************
 * @fn      free_alarm_node
 *
 * @brief   删除一个闹钟节点
 *
 * @param   alarm_head:头结点
 *
 * @return  成功返回0 失败返回-1
 */
int free_alarm_node(alarm_task_head_t *alarm_head, int alam_id)
{
    int res = -1;
    alarm_task_node_t *node;
    alarm_task_node_t *last_node;

    if(alarm_head==NULL || alarm_head->head==NULL)
    {
        return res;
    }

    node      = alarm_head->head;
    last_node = alarm_head->head;

    while(node!=NULL)
    {
        if(node->alarm_info.alarm_id==alam_id)
        {
            if(alarm_head->head == node)
            {
                alarm_head->head = node->next;
                free(node);
                break;
            }
            else
            {
                last_node->next = node->next;
                free(node);
                break;
            }
        }
        else
        {
            last_node = node;
            node      = node->next;
        }
    }

}

/*********************************************************************
 * @fn      free_alarm_task
 *
 * @brief   释放alarm_task链表
 *
 * @param   alarm_head:头结点
 *
 * @return  none
 */
void free_alarm_task(alarm_task_head_t *alarm_head)
{
    alarm_task_node_t *node;
    alarm_task_node_t *next_node;

    if(alarm_head==NULL||alarm_head->head==NULL)
    {
        return ;
    }

    if(alarm_head->head==NULL)
    {
        free(alarm_head);
        return ;
    }

    node      = alarm_head->head;
    next_node = node->next;
    while(next_node!=NULL)
    {
        free(node);
        node      = next_node;
        next_node = node->next;
    }
    free(node);
	free(alarm_head);
}
